< previous page page_675 next page >

Page 675
Procedure Call Versus In-line Code
Every time a function or procedure is called, some overhead is incurred. If you have a two-line function thats called from only three places, you might consider putting those two lines in the functions themselves and not making them a function. This can save some processing time. However, this can lead to some maintenance problems downstream. In the following example, a procedure is called to perform some calculation:
9f31265bb40b592b1929e18a47ab28e5.gif
For lngLoop = 0 To conRepeats
   Call InlineCalc
Next
In the next example, the same code in the procedure is moved right into the loop:
9f31265bb40b592b1929e18a47ab28e5.gif
For lngLoop = 0 To conRepeats
   intOp1 = 100
   intOp2 = 10
   intResult = intOp1 / intOp2
Next
Because you dont have the overhead of the procedure call, this code will execute faster.
Cached Properties Versus Not Cached
Retrieving values from properties is very slow. Avoid using properties in loops unless those properties are changing. Heres an example:
9f31265bb40b592b1929e18a47ab28e5.gif
For intLoop1 = 0 To Screen.FontCount - 1
   For intLoop2 = 0 To Printer.FontCount - 1
      If Screen.Fonts(intLoop1) = Printer.Fonts(intLoop2) Then
         strGetFont = Screen.Fonts(intLoop1)
      End If
   Next intLoop2
Next intLoop1
The inner loop retrieves the value from the Printer.FontCount property every time through the outer loop. This value does not change, so you should move it outside both the loops. In addition, you reference Screen.Fonts(intLoop1) two times within the inner loop. Again, this value is not changing, so you should cache it to a local variable prior to entering the inner loop. Heres an example:

 
< previous page page_675 next page >

If you like this book, buy it!